/**
 * 我的收藏
 * @author  Jello
 * @create  2018/11/05 09:10
 * @version 1.0
 * @company 广东蓝聘科技网络有限公司
 **/
var re_Time = ""; //返费时间
var re_Money = ""; //返费金额
var interview_Time = ""; //返费金额
var mechanism_Id = ""; //企业id
var work_Id = ""; //招聘id
var recruit_Address = ""; //面试地址
var mechanism_Name = ""; //企业名称
var curPage = 1;
var size = 3;
var key = "";
var token = "";
var isName = "";
var collectKeyWord = "";
var spinner = new Spinner(opts);

$(function() {
	token = sessionStorage.getItem("user_token") == null ? "" : sessionStorage.getItem("user_token");

	collectKeyWord = sessionStorage.getItem("collectKeyWord") == undefined ? "" : sessionStorage.getItem("collectKeyWord");
	$("#key").val(collectKeyWord);

	$("#nickname").html(sessionStorage.getItem("nickname"));

	//菜单栏切换
	$(".info").click(function() {
		$(".tab_nav").toggle();
		$("#menubar").attr("src", $(".tab_nav").is(":hidden") ? "img/personal/arrow_bottom.png" : "img/personal/arrow_top.png");
	});

	$(".errcover").click(function() {
		$(".errorbox").css("display", "none");
		$(document).unbind("scroll");
	})
	$(".cover").click(function() {
		$(".warnbox").css("display", "none");
		$(document).unbind("scroll");
	})

	$(".no").click(function() {
		$(".applybox").css("display", "none");
		$(document).unbind("scroll");
	})

	//报名
	$("#apply_btn").click(function() {
		var name = $("#applyname").val();
		var flag = checkApplyName(name);
		if(flag) {
			var param = {};
			param.reTime = re_Time;
			param.reMoney = re_Money;
			param.identy = "";
			param.interviewTime = interview_Time;
			param.mechanismId = mechanism_Id;
			param.userName = name;
			param.workId = work_Id;
			param.recruitAddress = recruit_Address;
			param.mechanismName = mechanism_Name;
			param.type = 0;
			param.token = token;
			entryApply(param, function(resultObj) {
				$(".applybox").hide();
				if(resultObj.code == 0 && resultObj.data != null) {
					setTimeout(function() {
						$(document).unbind("scroll");
						curPage = $(".current").html() == undefined ? "1" : $(".current").html();
						flipOverPage(curPage);
					}, 800);
				} else if(resultObj.code == 10002) {
					window.location.href = "login.html";
				} else {
					$(".warnbox").css("display", "block");
					$(".warntext").html(resultObj.msg);
					return;
				}
			});
		}
	})

	$("#applyname").click(function() {
		$(".error").hide();
	})

	flipOverPage(curPage);

	//搜索
	$("#search_btn").click(function() {
		key = $("#key").val();
		sessionStorage.setItem("collectKeyWord", key);
		$.ajax({
			type: "get",
			url: serverUrl + "/workapi/get_work_collection_api?userToken=" + token + "&size=" + size + "&key=" + key + "&status=1",
			cache: false,
			async: false,
			contentType: "application/json; charset=utf-8",
			timeout: 15000,
			dataType: "json",
			crossDomain: true,
			beforeSend: function() {
				$("#myspin").text("");
				var target = $("#myspin").get(0);
				spinner.spin(target);
			},
			success: function(data) {
				closeSpin(300);
				var resultObj = eval(data);
				$(".collect_con").empty();
				if(resultObj.code == 0 && resultObj.data.list.length != 0) {
					var workList = resultObj.data.list;
					for(var i = 0; i < workList.length; i++) {
						var workType = ""; //工种
						var lendType = ""; //借支
						var showtype1 = "";
						var showtype2 = "";
						var key = ""; //关键字
						var money = "";
						var lend_type = workList[i].lendType;
						var post_type = workList[i].postType;

						if(lend_type == 1) {
							lendType = "可借支";
						}
						if(post_type == 1) {
							workType = "小时工";
							money = workList[i].workMoney + '元/小时';
						} else if(post_type == 0) {
							workType = "正式工";
							money = workList[i].wageRange + '元/月';
						}
						if(workList[i].isApply == 0) {
							showtype1 = "block";
							showtype2 = "none";
						} else {
							showtype1 = "none";
							showtype2 = "block";
						}
						if(workList[i].key != null && workList[i].key != "") {
							key = workList[i].key;
						}

						reTime = workList[i].reTime;
						reMoney = workList[i].reMoney;
						mechanismId = workList[i].mechanismId;
						workId = workList[i].id;
						recruitAddress = workList[i].recruitAddress;
						mechanismName = workList[i].mechanismName;
						interviewTime = workList[i].interviewTime;

						var collect_html = '<div class="collect_info"><div class="collect_main">' +
							'<div class="collect_left"><div class="collect_tit1">' +
							'<div class="tit1_left"><img src="' + workList[i].mechanismUrl + '" /><span class="company">' + workList[i].mechanismName + '</span>' +
							'<span class="key">' + key + '</span></div>' +
							'<div class="tit1_center"><span class="post">' + workType + '</span><span class="score">' + workList[i].mechanismScore + '分</span></div></div>' +
							'<div class="collect_tit2"><span class="work">需' + workList[i].workTypeName + '' + workList[i].maxNumber + '人</span><span class="money">' + money + '</span>' +
							'<span class="lend">' + lendType + '</span><span class="sureapply">已报名' + workList[i].applyNumber + '人</span></div></div>' +
							'<div class="collect_right"><div class="collect_btn"><button type="button" class="apply2" style="display: ' + showtype1 + ';" disabled="true">已报名</button>' +
							'<button type="button" class="apply" style="display: ' + showtype2 + ';" onclick="apply(' + reTime + ',' + reMoney + ',' + mechanismId + ',' + workId + ',&quot' + recruitAddress + '&quot,&quot' + mechanismName + '&quot,\'' + interviewTime + '\')">报名</button></div>' +
							'<div class="collect_btn"><button type="button" class="delete" onclick="deleteCollect(' + workList[i].collectId + ')">删除</button></div></div>' +
							'</div></div>';

						$(".collect_con").append(collect_html);

					}
					$(".collect_con").show();
					$(".collect_empty").hide();

					if(resultObj.data.totalSize > 3) {
						//分页插件
						$(".collect_page").show();
						$("#page").paging({
							pageNo: resultObj.data.pageNum,
							totalPage: resultObj.data.totalPages,
							totalSize: resultObj.data.totalSize,
							callback: function(num) {
								flipOverPage(num);
							}
						})
					} else {
						$(".collect_page").hide();
					}
				} else if(resultObj.code == 10002) {
					window.location.href = "login.html";
				} else { //沒有收藏
					$(".collect_empty").show();
					$(".collect_con").hide();
					$(".collect_page").hide();
					$("#emptyimg").attr("src", "img/collect/search_empty.png");
					$("#emptymsg").html("没有找到相关内容，换个关键词试试吧！");
				}
			}
		});
	})

	//根据关键字或公司查询
	$("#searchJob").click(function() {
		var companyName = $("#companyKey").val();
		sessionStorage.setItem("keyword1", companyName);
		window.location.href = "applyjob.html";
	})
	$("#companyKey").keyup(function(event) {
		if(event.keyCode == 13) {
			var companyName = $("#companyKey").val();
			sessionStorage.setItem("keyword", companyName);
			window.location.href = "applyjob.html";
		}
	});

	//客服中心
	$("#service_btn1").click(function() {
		window.open("http://wpa.qq.com/msgrd?v=3&uin=1423335266&site=qq&menu=yes");
	})
	$("#service_btn2").click(function() {
		window.open("http://wpa.qq.com/msgrd?v=3&uin=2028048473&site=qq&menu=yes");
	})
	$("#service_btn3").click(function() {
		window.open("http://wpa.qq.com/msgrd?v=3&uin=3341118684&site=qq&menu=yes");
	})

})

/**
 * 校验真实姓名
 * @param {Object} name
 */
function checkApplyName(name) {
	var reg = /^[\u2E80-\u9FFF]+$/; //中文
	if(name == null || name == "") {
		$(".error_txt").html("请输入您的姓名");
		$(".error").show();
		return;
	} else if(!reg.test(name)) {
		$(".error_txt").html("请输入您的中文姓名");
		$(".error").show();
		return;
	}
	return true;
}

/**
 * 查询收藏的招聘 
 */
function flipOverPage(curPage) {
	key = $("#key").val();
	$.ajax({
		type: "get",
		url: serverUrl + "/workapi/get_work_collection_api?userToken=" + token + "&page=" + curPage + "&size=" + size + "&key=" + key + "&status=1",
		cache: false,
		async: false,
		contentType: "application/json; charset=utf-8",
		timeout: 15000,
		dataType: "json",
		crossDomain: true,
		beforeSend: function() {
			$("#myspin").text("");
			var target = $("#myspin").get(0);
			spinner.spin(target);
		},
		success: function(data) {
			closeSpin(300);
			var resultObj = eval(data);
			$(".collect_con").empty();
			if(resultObj.code == 0 && resultObj.data.list.length != 0) {
				var workList = resultObj.data.list;
				var workType = ""; //工种
				var lendType = ""; //借支
				var showtype1 = "";
				var showtype2 = "";
				var key = ""; //关键字

				for(var i = 0; i < workList.length; i++) {

					var lend_type = workList[i].lendType;
					var post_type = workList[i].postType;
					var money = "";
					if(lend_type == 1) {
						lendType = "可借支";
					}
					if(post_type == 1) {
						workType = "小时工";
						money = workList[i].workMoney + '元/小时';
					} else if(post_type == 0) {
						workType = "正式工";
						money = workList[i].wageRange + '元/月';
					}
					if(workList[i].isApply == 0) {
						showtype1 = "block";
						showtype2 = "none";
					} else {
						showtype1 = "none";
						showtype2 = "block";
					}
					if(workList[i].key != null && workList[i].key != "") {
						key = workList[i].key;
					}

					reTime = workList[i].reTime;
					reMoney = workList[i].reMoney;
					mechanismId = workList[i].mechanismId;
					workId = workList[i].id;
					recruitAddress = workList[i].recruitAddress;
					mechanismName = workList[i].mechanismName;
					interviewTime = workList[i].interviewTime;

					var collect_html = '<div class="collect_info"><div class="collect_main">' +
						'<div class="collect_left"><div class="collect_tit1">' +
						'<div class="tit1_left"><img src="' + workList[i].mechanismUrl + '" /><span class="company">' + workList[i].mechanismName + '</span>' +
						'<span class="key">' + key + '</span></div>' +
						'<div class="tit1_center"><span class="post">' + workType + '</span><span class="score">' + workList[i].mechanismScore + '分</span></div></div>' +
						'<div class="collect_tit2"><span class="work">需' + workList[i].workTypeName + '' + workList[i].maxNumber + '人</span><span class="money">' + money + '</span>' +
						'<span class="lend">' + lendType + '</span><span class="sureapply">已报名' + workList[i].applyNumber + '人</span></div></div>' +
						'<div class="collect_right"><div class="collect_btn"><button type="button" class="apply2" style="display: ' + showtype1 + ';" disabled="true">已报名</button>' +
						'<button type="button" class="apply" style="display: ' + showtype2 + ';" onclick="apply(' + reTime + ',' + reMoney + ',' + mechanismId + ',' + workId + ',\'' + recruitAddress + '\',\'' + mechanismName + '\',\'' + interviewTime + '\')">报名</button></div>' +
						'<div class="collect_btn"><button type="button" class="delete" onclick="deleteCollect(' + workList[i].collectId + ')">删除</button></div></div>' +
						'</div></div>';

					$(".collect_con").append(collect_html);
				}

				if(resultObj.data.totalSize > 3) {
					//分页插件
					$(".collect_page").show();
					$("#page").paging({
						pageNo: resultObj.data.pageNum,
						totalPage: resultObj.data.totalPages,
						totalSize: resultObj.data.totalSize,
						callback: function(num) {
							curPage = num;
							flipOverPage(curPage);
						}
					})
				} else {
					$(".collect_page").hide();
				}
			} else if(resultObj.code == 10002) {
				window.location.href = "login.html";
			} else { //沒有收藏
				$(".collect_empty").css("display", "block");
				$(".collect_con").hide();
				$(".collect_page").hide();
				$("#emptymsg").html("空空如也，快去收藏吧！");
			}
		}
	});
}

/**
 * 入职报名
 */
function apply(reTime, reMoney, mechanismId, workId, recruitAddress, mechanismName, interviewTime) {
	re_Time = reTime;
	re_Money = reMoney;
	mechanism_Id = mechanismId;
	work_Id = workId;
	recruit_Address = recruitAddress;
	mechanism_Name = mechanismName;
	interview_Time = interviewTime;

	//查询是否报名、收藏
	var param = {};
	param.workId = work_Id;
	param.token = token;
	param.login_status = "1";
	queryIsApplyOrCollect(param, function(resultObj) {
		isName = resultObj.userName;
	});
	if(isName) {
		var param = {};
		param.reTime = re_Time;
		param.reMoney = re_Money;
		param.identy = "";
		param.interviewTime = interview_Time;
		param.mechanismId = mechanism_Id;
		param.userName = isName;
		param.workId = work_Id;
		param.recruitAddress = recruit_Address;
		param.mechanismName = mechanism_Name;
		param.type = 0;
		param.token = token;
		entryApply(param, function(resultObj) {
			$(".applybox").hide();
			if(resultObj.code == 0 && resultObj.data != null) {
				setTimeout(function() {
					$(document).unbind("scroll");
					curPage = $(".current").html() == undefined ? "1" : $(".current").html();
					flipOverPage(curPage);
				}, 800);
			} else if(resultObj.code == 10002) {
				window.location.href = "login.html";
			} else {
				$(".warnbox").css("display", "block");
				$(".warntext").html(resultObj.msg);
				return;
			}
		});
	} else {
		stopScroll();
		$(".applybox").show();
	}
}

/**
 * 删除收藏的招聘
 * @param {Object} id
 */
function deleteCollect(id) {
	var collectId = id;
	$.ajax({
		type: "get",
		url: serverUrl + "/collection/delete_collection?userToken=" + token + "&status=1" + "&ids=" + collectId,
		cache: false,
		async: false,
		contentType: "application/json; charset=utf-8",
		dataType: "json",
		crossDomain: true,
		timeout: asktime,
		success: function(data) {
			var resultObj = eval(data);
			if(resultObj.code == 0 && resultObj.data != null) {
				$(".errortext").html("删除成功");
				$(".errorimg").attr("src", "img/icon/warn.png");
				$(".errorbox").show();
				setTimeout(function() {
					$(".errorbox").hide();
					$(document).unbind("scroll");
					flipOverPage(1);
				}, 800);
			} else if(resultObj.code == 10002) {
				window.location.href = "login.html";
			} else {
				$(".errorbox").show();
				$(".errortext").val("删除失败");
				return;
			}
		},
		error: function(XMLHttpRequest, textStatus, errorThrown) {
			$(".errorbox").css("display", "block");
			$(".errortext").val("网络异常");
			return;
		}
	});
}